set_constant_variables()
{
    scriptpath="$(readlink -f "$0")"
    top_dir="${scriptpath%/${0##*/}}"
    targets_dir="$top_dir/targets"
    patches_dir="$top_dir/patches-generic"
}
set_version_variables()
{
    branch_name="Chaos Calmer"
    branch_id="15.05"
    branch_is_trunk="0"
    branch_packages_path="packages"
    openwrt_commit="03d52cfcff87c0e8e09e7a455a6fdefb7138e369"
    openwrt_abbrev_commit=$(echo "$openwrt_commit" | cut -b 1-7)
    build_date=$(date +"%B %d, %Y")
    gargoyle_git_revision=$(git log -1 --pretty=format:$h)
    	if [ -z "$full_gargoyle_version" ] ; then
		full_gargoyle_version="$1"
	fi
	if [ -z "$full_gargoyle_version" ] ; then
		full_gargoyle_version="Unknown"
	fi
    	short_gargoyle_version=$(echo "$full_gargoyle_version" | awk '{ print $1 ; }' | sed 's/[^0-9^A-Z^a-z^\.^\-^_].*$//g' )
    	lower_short_gargoyle_version=$(echo "$short_gargoyle_version" | tr 'A-Z' 'a-z' )
	numeric_gargoyle_version=$(echo "$short_gargoyle_version" | sed 's/[Xx]/0/' )
	not_numeric=$(echo "$numeric_gargoyle_version" | sed 's/[\.0123456789]//g')
	if [ -n "$not_numeric" ] ; then
		numeric_gargoyle_version="0.9.9"
	fi
}
#获取CONFIG_TARGET_BOARD的值
get_target_from_config()
{
	config_file_path="$1"
	cat .config | grep "^CONFIG_TARGET_BOARD=" | sed 's/\"$//g' | sed 's/^.*\"//g'
}
distrib_init ()
{
	if [ ! -d "$top_dir/Distribution" ] ; then
		mkdir "$top_dir/Distribution"
	fi
	#git log --since=5/16/2013 $(git log -1 --pretty=format:%h) --pretty=format:"%h%x09%ad%x09%s" --date=short > "$top_dir/Distribution/changelog.txt"
	git log $(git describe --abbrev=0 --tags)..$(git log -1 --pretty=format:%h) --no-merges --pretty=format:"%h%x09%ad%x09%s" --date=short > "$top_dir/Distribution/Gargoyle changelog.txt"
	cp -fR "$top_dir/LICENSES" "$top_dir/Distribution/"
}

set_constant_variables

targets="ramips"
full_gargoyle_version="$2"
specified_profile="xdcctv" 	
num_build_threads="6"
num_build_thread_str=""
distribution="true"
if [ "$num_build_threads" = "single" ] ; then
	num_build_threads="1"
	num_build_thread_str="-j1"
elif [ "$num_build_threads" = "" ] || [ "$num_build_threads" = "auto" ] ; then
	num_build_threads="auto"
	num_build_thread_str=""
elif [ "$num_build_threads" -lt 1 ] ; then
	num_build_threads="1"
	num_build_thread_str="-j1"
else
	num_build_thread_str="-j$num_build_threads"
fi

set_version_variables "$full_gargoyle_version"

if [ -d "$top_dir/package-prepare" ] ; then
    rm -rf "$top_dir/package-prepare"
fi
if [ ! -d "$top_dir/downloaded" ] ; then
    mkdir "$top_dir/downloaded"
fi
openwrt_src_dir="$top_dir/downloaded/$branch_id"
openwrt_package_dir="$top_dir/downloaded/$branch_id-packages"
if [ -n "$openwrt_commit" ]; then
    openwrt_src_dir="$top_dir/downloaded/$branch_id-${openwrt_abbrev_commit}"
    openwrt_package_dir="$top_dir/downloaded/$branch_id-packages-${openwrt_abbrev_commit}"
else
    rm -rf "$openwrt_src_dir"
    rm -rf "$openwrt_package_dir"
fi
if [ ! -d "$openwrt_src_dir" ]; then
    rm -rf "$branch_name" "$branch_id"
    if [ "$branch_is_trunk" = "1" ] ; then
    echo "11$openwrt_src_dir"
		git clone  git://git.openwrt.org/openwrt.git "$openwrt_src_dir"
    else
        echo "22,$branch_id $openwrt_src_dir"
        # git clone  git://git.openwrt.org/$branch_id/openwrt.git "$openwrt_src_dir"
    fi
    if [ ! -d "$openwrt_src_dir" ] ; then #下载源码失败
    echo "ERROR: could not download source,exiting"    
    exit
    fi
    if [ -n "$openwrt_commit" ] ; then #切换到特定的提交点
        cd "$openwrt_src_dir"
        git checkout "$openwrt_commit"
    fi
    cd "$top_dir"
    mv "$branch_id" "$openwrt_src_dir"
fi
echo "$top_dir,$brancn_id,$openwrt_src_dir"
rm -rf "$openwrt_src_dir/dl"
ln -s "$top_dir/downloaded" "$openwrt_src_dir/dl"
for target in $targets ; do
    rm -rf "$target-src"
    if [ -z "$specified_profile" ] ; then
        rm -rf "$top_dir/built/$target"
        rm -rf "$top_dir/images/$target"
    else
        profile_images=$(cat "$targets_dir/$target/profiles/$specified_profile/profile_images" 2>/dev/null)
        mkdir -p "$top_dir/images/$target/"
        for pi in $profile_images ; do
            rm -rf "$top_dir/images/$target/"*"$pi"*
        done
    fi
     cp -r "$openwrt_src_dir" "$target-src" #复制openwrt
     package_dir="$top_dir/package-prepare"
     if [ ! -d "$package_dir" ] ; then
        package_dir="$top_dir/package"
     fi

     gargoyle_packages=$(ls "$package_dir")
     for gp in $gargoyle_packages ; do
        IFS_ORIG="$IFS"
        IFS_LINEBREAK="$(printf '\n\r')"
        IFS="$IFS_LINEBREAK"
        matching_packages=$(find "$target-src/package" -name "$gp")
        for mp in $matching_packages ; do
            if [ -d "$mp" ] && [ -e "$mp/Makfile" ]; then
                rm -rf "$mp"
            fi
        done
        IFS="$IFS_ORIG"
        cp -r "$package_dir/$gp" "$target-src/package"
     done
     default_profile="default"
     if [ -n "$specified_profile" ] ; then
        default_profile="$specified_profile"
     fi
     profile_target_dir="$target"
     if [ "$target" = "custom" ] && [ -n "custom_template" ] ; then
        profile_target_dir="$custom_template"
     fi
     if [ ! -e "$targets_dir/$profile_target_dir/profiles/$default_profile/config" ]; then
        profile_dir=""
        profile_dirs="$targets_dir/$profile_target_dir/profiles"/*
        for pd in $profile_dirs ; do
            if [ -z "$profile_dir" ] && [ -e "$pd/config" ] ; then
                profile_dir="$pd"
                default_profile=$(echo "$profile_dir" | sed 's/^.*\///g'| sed 's/^.*\\//g')
            fi
        done
     fi
     profile_name="$default_profile"
     if [ "$target" = "custom" ] ; then
        profile_name = "custom"
     fi
    cp "$targets_dir/$target/profiles/$default_profile/config" "$top_dir/${target}-src/.config"


     if [ "$target" = "custom" ] && [ "$default_profile" = "default" ] ; then
        echo "custom" 
     fi
     cd "$top_dir/$target-src"
     find . -name "*~" | xargs rm -rf
     find . -name ".*sw*" | xargs rm -rf
    # echo "OFFICIAL_VERSION:=$full_gargoyle_version" > .ver

    ## verbosity省略,输出所有的人内容
    echo "$patches_dir"
    exit 1
    openwrt_target=$(get_target_from_config "./.config")
    make $num_build_thread_str V=99 
    if [ "$distribution" = "true" ] || ["&distribution" = "TRUE" ] ||[ "$distribution" = "1" ] ; then
        distribution = "true"
        distrib_init
        mkdir -p "$top_dir/Distribution/Images/$target-$default_profile"
    fi
done
